using System;
using System.Collections.Generic;
using System.Text;
using Emlyn.Common.Data.DBUpdater;
using System.Data.SqlClient;
using Emlyn.Common.Data.SqlServer.SqlManagers;
using Emlyn.Website.Users;

namespace Emlyn.Website.EmlynORegan.DBUpdateRules
{
    public class DUR_0010_tblEOWebsite: IDBUpdaterRule
    {
        #region IDBUpdaterRule Members

        public int UpgradeFromVersion()
        {
            return 0;
        }

        public int UpgradeToVersion()
        {
            return 10;
        }

        public void PerformUpgrade()
        {
            string ltblEOWebsite = 
                "CREATE TABLE [dbo].[tblEOWebsite] ( " +
                "   [WebsiteCode] [char] (20) COLLATE Latin1_General_CI_AS NOT NULL , "+
                "	[DBVersion] [int] NOT NULL " +
                ") ";

            SqlManager.Execute(ltblEOWebsite);

            string ltblEOWebsitePK = 
                "ALTER TABLE [dbo].[tblEOWebsite] WITH NOCHECK ADD " +
                "	CONSTRAINT [PK_tblEOWebsite] PRIMARY KEY  CLUSTERED " +
                "	( " +
                "		[WebsiteCode] " +
                "	) ";
            SqlManager.Execute(ltblEOWebsitePK);

            string ltblEOWebsiteVersionDefault =
                "ALTER TABLE [dbo].[tblEOWebsite] ADD " +
                "	CONSTRAINT [DF_tblEOWebsite_DBVersion] DEFAULT (0) FOR [DBVersion] ";
            SqlManager.Execute(ltblEOWebsiteVersionDefault);

            // now we can use the user & website layer to add a record
            // for this website.

            EWebsite lwebsite = new EWebsite();
            lwebsite.WebsiteCode = DBUpdaterMain.WebsiteCode;
            lwebsite.Version = 10;

            EWebsiteMod lwebsiteMod = new EWebsiteMod();
            lwebsiteMod.Save(lwebsite);
        }

        #endregion
    }
}
